package com.yj.lottery_system.service.activityStatus.operator;

import com.yj.lottery_system.dao.dataObject.ActivityPrizeDO;
import com.yj.lottery_system.dao.dataObject.ActivityUserDO;
import com.yj.lottery_system.dao.mapper.ActivityPrizeMapper;
import com.yj.lottery_system.dao.mapper.ActivityUserMapper;
import com.yj.lottery_system.service.dto.ConvertActivityStatusDTO;
import com.yj.lottery_system.service.enums.ActivityStatusEnum;
import com.yj.lottery_system.service.enums.ActivityUserStatusEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component

public class UserOperator extends AbstractActivityOperator{
   @Resource
   private ActivityUserMapper activityUserMapper;
    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        //活动id
        Long activityId = convertActivityStatusDTO.getActivityId();
        //人员id列表
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        //目标状态
        ActivityUserStatusEnum targetActivityUserStatus = convertActivityStatusDTO.getTargetActivityUserStatus();

        if(null == activityId
                || CollectionUtils.isEmpty(userIds)
                || null == targetActivityUserStatus) {
            return false;
        }
        //调用dao
        List<ActivityUserDO> activityUserDOList = activityUserMapper.batchSelectByAUId(activityId,userIds);

        if(CollectionUtils.isEmpty(activityUserDOList)) {
            return false;
        }
        //判断中奖者状态 与目标状态
        for(ActivityUserDO activityUserDO : activityUserDOList ) {
            if(activityUserDO.getStatus().equalsIgnoreCase(targetActivityUserStatus.name())) {
                return false;
            }
        }

        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {

        //活动id
        Long activityId = convertActivityStatusDTO.getActivityId();
        //人员id列表
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        //目标状态
        ActivityUserStatusEnum targetActivityUserStatus = convertActivityStatusDTO.getTargetActivityUserStatus();

        try{
            activityUserMapper.batchUpdateStatus(activityId, userIds,targetActivityUserStatus.name());
            return true;
        }catch (Exception e) {
            return false;
        }
    }
}
